package com.wetao.module_report;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.wetao.lib_common.ConstantArouter;
import com.wetao.lib_common.base.BaseActivity;
import com.wetao.lib_common.base.BaseResponse;
import com.wetao.lib_common.bean.response.SingleStudyInfo;
import com.wetao.lib_common.contract.EmptyCon;
import com.wetao.lib_common.presenter.EmptyPre;
import com.wetao.lib_common.tsdBlePen.TsdService;
import com.wetao.lib_common.utils.ComUtil;
import com.wetao.lib_common.utils.LogUtils;
import com.wetao.lib_common.utils.StringUtil;
import com.wetao.lib_common.view.HorizontalChartView.Chart;
import com.wetao.lib_common.view.HorizontalChartView.HorizontalChartView;
import com.wetao.module_report.contract.ReportSingleDetailCon;
import com.wetao.module_report.presenter.ReportSingleDetailPre;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ConstantArouter.PATH_REPORT_SINGLE_DETAIL_ACTIVITY)
public class ReportSingleDetailActivity extends BaseActivity<ReportSingleDetailCon.View, ReportSingleDetailPre>
        implements ReportSingleDetailCon.View {

    @Autowired
    int subjectId;
    @Autowired
    String subjectName;
    @Autowired
    int examId;
    @Autowired
    String examName;
    @Autowired
    int publishId;

    @BindView(R2.id.tv_title)
    TextView tv_title;
    @BindView(R2.id.tv_examName)
    TextView tv_examName;
    @BindView(R2.id.tv_myRate)
    TextView tv_myRate;
    @BindView(R2.id.tv_totalScore)
    TextView tv_totalScore;
    @BindView(R2.id.tv_classAvRate)
    TextView tv_classAvRate;
    @BindView(R2.id.tv_ranking)
    TextView tv_ranking;
    @BindView(R2.id.tv_previous_ranking)
    TextView tv_previous_ranking;
    @BindView(R2.id.tv_tiliang)
    TextView tv_tiliang;
    @BindView(R2.id.tv_dadui)
    TextView tv_dadui;
    @BindView(R2.id.tv_dacuo)
    TextView tv_dacuo;
    @BindView(R2.id.tv_unanswerCount)
    TextView tv_unanswerCount;
    @BindView(R2.id.tv_uncorrectCount)
    TextView tv_uncorrectCount;
    @BindView(R2.id.tv_my_fen)
    TextView tv_my_fen;
    @BindView(R2.id.tv_my_miao)
    TextView tv_my_miao;
    @BindView(R2.id.tv_class_fen)
    TextView tv_class_fen;
    @BindView(R2.id.tv_class_miao)
    TextView tv_class_miao;
    @BindView(R2.id.ch_view)
    HorizontalChartView ch_view;
    @BindView(R2.id.tv_jy1)
    TextView tv_jy1;
    @BindView(R2.id.iv_jy1)
    ImageView iv_jy1;
    @BindView(R2.id.tv_jy1_tag)
    TextView tv_jy1_tag;
    @BindView(R2.id.tv_jy2)
    TextView tv_jy2;
    @BindView(R2.id.iv_jy2)
    ImageView iv_jy2;
    @BindView(R2.id.tv_jy2_tag)
    TextView tv_jy2_tag;
    @BindView(R2.id.tv_jy3)
    TextView tv_jy3;
    @BindView(R2.id.iv_jy3)
    ImageView iv_jy3;
    @BindView(R2.id.tv_jy3_tag)
    TextView tv_jy3_tag;
    @BindView(R2.id.tv_jy4)
    TextView tv_jy4;
    @BindView(R2.id.iv_jy4)
    ImageView iv_jy4;
    @BindView(R2.id.tv_jy4_tag)
    TextView tv_jy4_tag;
    @BindView(R2.id.ll_haveData)
    LinearLayout ll_haveData;
    @BindView(R2.id.ll_noData)
    LinearLayout ll_noData;
    @BindView(R2.id.tv_no_data)
    TextView tv_no_data;

    SingleStudyInfo info;



    @Override
    public int getLayoutId() {
        return R.layout.report_single_detail_activity;
    }

    @Override
    public boolean getIsBindTsdService() {
        return false;
    }

    @Override
    public TsdService.OnDataReceiveListener getTsdServiceDataReceiveListener() {
        return null;
    }

    @Override
    public ReportSingleDetailPre createPresenter() {
        return new ReportSingleDetailPre(this);
    }

    @Override
    public ReportSingleDetailCon.View createView() {
        return this;
    }

    @Override
    public void querySingleStudyInfoResult(BaseResponse<SingleStudyInfo> resp) {
        if(resp.getCode() == 0) {
            ll_haveData.setVisibility(View.VISIBLE);
            ll_noData.setVisibility(View.GONE);

            info = resp.getData();
            refreshUI();
        } else {
            ll_haveData.setVisibility(View.GONE);
            ll_noData.setVisibility(View.VISIBLE);
            tv_no_data.setText(resp.getMsg());
            //tv_no_data.setText("暂无学情信息");
        }

    }

    @Override
    public void querySingleStudyInfoFailed() {
    }

    @Override
    public void init() {
        LogUtils.i("--> subjectId=" + subjectId + ", subjectName=" + subjectName + ", examId=" + examId + ", examName=" + examName + ", publishId=" + publishId);

        tv_title.setText(subjectName + "学情报告");

        ch_view.setMaxScore(100);

        getPresenter().querySingleStudyInfo(ComUtil.getHashmapByStr(
                new String[]{"publishId"},
                new String[]{publishId + ""}), false, false);


    }

    @OnClick({R2.id.iv_back
    })
    public void onClickView(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        }
    }

    public List<Chart> getCharts() {
        List<Chart> list = new ArrayList<>();
        Chart chart = new Chart();
        chart.setLeftName("错题率");
        chart.setRightName(float2float(info.getWrongRatio().floatValue()));
        Chart chart1 = new Chart();
        chart1.setLeftName("班级平均错题率");
        chart1.setRightName(float2float(info.getClassWrongRatio().floatValue()));
        list.add(chart1);
        list.add(chart);
        return list;
    }

    private String float2Str(Float f) {
        if(f == null) {
            return "0";
        }

        BigDecimal a= new BigDecimal(f);
        BigDecimal j = a.multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
        return j.stripTrailingZeros().toPlainString();

        /*int i = (int)(f.floatValue() * 10000);
        int i_a = i / 100;
        int i_b = i % 100;
        if(i_b == 0) {
            return i_a + "";
        } else {
            return  i_a + "." + i_b;
        }*/
    }

    private float float2float(Float f) {
        if(f == null) {
            return 0;
        }

        BigDecimal a= new BigDecimal(f);
        BigDecimal j = a.multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
        return j.stripTrailingZeros().floatValue();
    }

    private String float2Str_n(Float f) {
        if(f == null) {
            return "0";
        }
        BigDecimal a= new BigDecimal(f);
        BigDecimal j = a.setScale(2, BigDecimal.ROUND_HALF_UP);
        return j.stripTrailingZeros().toPlainString();
    }

    private void refreshUI() {
        tv_examName.setText("作业名称：" + StringUtil.replaceAmp(info.getExamName()));
        tv_myRate.setText(float2Str(info.getScoreRatio()) +"%");
        if(info.getScoreRatio().floatValue() >= info.getClassScoreAverageRatio().floatValue()) {
            tv_myRate.setTextColor(Color.parseColor("#ff00a8b2"));
        } else {
            tv_myRate.setTextColor(Color.parseColor("#FF3A51"));
        }
        tv_totalScore.setText("得分率(总分" + float2Str_n(info.getScoreTotal()) + "分)");
        tv_classAvRate.setText(float2Str(info.getClassScoreAverageRatio()) + "%");
        tv_ranking.setText(info.getClassRank() + "");
        if(info.getPreviousRank() != null && info.getClassRank().intValue() > info.getPreviousRank().intValue()) {
            tv_ranking.setTextColor(Color.parseColor("#FF3A51"));
        } else {
            tv_ranking.setTextColor(Color.parseColor("#ff00a8b2"));
        }
        if(info.getPreviousRank() != null) {
            tv_previous_ranking.setText("（上次排名" + info.getPreviousRank().intValue() + "）");
        } else {
            tv_previous_ranking.setText("（上次排名-）");
        }
        tv_tiliang.setText(info.getQuestionCount() + "");
        tv_dadui.setText(info.getRightCount() + "");
        tv_dacuo.setText(info.getWrongCount() + "");
        tv_unanswerCount.setText(info.getUnanswerCount() + "");
        tv_uncorrectCount.setText(info.getUncorrectCount() + "");
        tv_my_fen.setText(info.getAnswerTime().intValue() / 60 + "");
        tv_my_miao.setText(info.getAnswerTime().intValue() % 60 + "");
        tv_class_fen.setText(info.getClassAnswerTime().intValue() / 60 + "");
        tv_class_miao.setText(info.getClassAnswerTime().intValue() % 60 + "");
        ch_view.setMaxScore(100);
        ch_view.setData(getCharts());
        float diff;
        String diff_str;
        int resId;
        String diff_tag;
        //diff = ((int)((info.getScoreRatio().floatValue() - info.getClassScoreAverageRatio().floatValue()) * 10000))/100f;
        diff = float2float(info.getScoreRatio().floatValue() - info.getClassScoreAverageRatio().floatValue());
        if(diff > 0) {
            diff_str = "高" + diff + "%";
            tv_jy1.setTextColor(Color.parseColor("#ff00a8b2"));
            resId = R.drawable.report_xqbg_xxjy1;
            diff_tag = "表现不错，继续保持！";
        } else if(diff < 0) {
            diff_str = "低" + (-diff) + "%";
            tv_jy1.setTextColor(Color.parseColor("#FF3A51"));
            resId = R.drawable.report_xqbg_xxjy3;
            diff_tag = "表现较差，努力加油！";
        } else {
            diff_str = "-持平";
            tv_jy1.setTextColor(Color.parseColor("#ff00a8b2"));
            resId = R.drawable.report_xqbg_xxjy2;
            diff_tag = "表现尚可，有待提升！";
        }
        tv_jy1.setText(diff_str);
        iv_jy1.setImageResource(resId);
        tv_jy1_tag.setText(diff_tag);

        if(info.getPreviousRank() == null) {
            diff = 0;
        } else {
            diff = info.getClassRank().intValue() - info.getPreviousRank().intValue();
        }
        if(diff > 0) {
            diff_str = "下降" + (int)diff + "名";
            tv_jy2.setTextColor(Color.parseColor("#FF3A51"));
            resId = R.drawable.report_xqbg_xxjy3;
            diff_tag = "表现较差，努力加油！";
        } else if(diff < 0) {
            diff_str = "上升" + (int)(-diff) + "名";
            tv_jy2.setTextColor(Color.parseColor("#ff00a8b2"));
            resId = R.drawable.report_xqbg_xxjy1;
            diff_tag = "表现不错，继续保持！";
        } else {
            diff_str = "-持平";
            tv_jy2.setTextColor(Color.parseColor("#ff00a8b2"));
            resId = R.drawable.report_xqbg_xxjy2;
            diff_tag = "表现尚可，有待提升！";
        }
        tv_jy2.setText(diff_str);
        iv_jy2.setImageResource(resId);
        tv_jy2_tag.setText(diff_tag);


        //diff = ((int)((info.getWrongRatio().floatValue() - info.getClassWrongRatio().floatValue()) * 10000)) / 100f;
        diff = float2float(info.getWrongRatio().floatValue() - info.getClassWrongRatio().floatValue());
        if(diff > 0) {
            diff_str = "高" + diff + "%";
            tv_jy3.setTextColor(Color.parseColor("#FF3A51"));
            resId = R.drawable.report_xqbg_xxjy3;
            diff_tag = "表现较差，努力加油！";
        } else if(diff < 0) {
            diff_str = "低" + (-diff) + "%";
            tv_jy3.setTextColor(Color.parseColor("#ff00a8b2"));
            resId = R.drawable.report_xqbg_xxjy1;
            diff_tag = "表现不错，继续保持！";
        } else {
            diff_str = "-持平";
            tv_jy3.setTextColor(Color.parseColor("#ff00a8b2"));
            resId = R.drawable.report_xqbg_xxjy2;
            diff_tag = "表现尚可，有待提升！";
        }
        tv_jy3.setText(diff_str);
        iv_jy3.setImageResource(resId);
        tv_jy3_tag.setText(diff_tag);

        diff = info.getClassAnswerTime().intValue() - info.getAnswerTime().intValue();
        if(diff > 0) {
            int fen = (int)(diff/60);
            int miao = (int)(diff%60);
            diff_str = "快" + (fen > 0 ? (fen + "分") : "") + (miao > 0 ? (miao + "秒") : "");
            tv_jy4.setTextColor(Color.parseColor("#ff00a8b2"));
            resId = R.drawable.report_xqbg_xxjy1;
            diff_tag = "表现不错，继续保持！";
        } else if(diff < 0) {
            int fen = (int)(-diff/60);
            int miao = (int)(-diff%60);
            diff_str = "慢" + (fen > 0 ? (fen + "分") : "") + (miao > 0 ? (miao + "秒") : "");
            tv_jy4.setTextColor(Color.parseColor("#FF3A51"));
            resId = R.drawable.report_xqbg_xxjy3;
            diff_tag = "表现较差，努力加油！";
        } else {
            diff_str = "-持平";
            tv_jy4.setTextColor(Color.parseColor("#ff00a8b2"));
            resId = R.drawable.report_xqbg_xxjy2;
            diff_tag = "表现尚可，有待提升！";
        }
        tv_jy4.setText(diff_str);
        iv_jy4.setImageResource(resId);
        tv_jy4_tag.setText(diff_tag);
    }
}
